
package pe.edu.pucp.Maestros.DAO;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pe.edu.pucp.Modelo.Avion;
import pe.edu.pucp.Util.HibernateUtil;
/**
 *
 * @author oscar
 */
public class AvionDAO {
    

    public void guardarAvion(Avion c) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        session.save(c);
        tx.commit();
        session.close();
    }

    public ArrayList<Avion> listarAviones() {
        ArrayList<Avion> lstAviones = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            List lista = session.createQuery("from Avion").list();
            for ( Iterator iter = lista.iterator(); iter.hasNext(); ) {    
                Avion a = (Avion) iter.next();
                lstAviones.add(a);
            }

            tx.commit();
        } catch (Exception e) { 
           if (tx != null) {
             tx.rollback();
             throw e;
           }
        }  finally { 
             session.close();
            return lstAviones;
        }
    }
    
       
    public Avion listarUnAvion(int idavion) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        Avion a= new Avion();
        try {
            tx = session.beginTransaction();

            List lista = session.createQuery("from Avion where IdAvion="+idavion).list();

            for ( Iterator iter = lista.iterator(); iter.hasNext(); ) {    
                a = (Avion) iter.next();
                Hibernate.initialize(a.getEstadoAvion());
                Hibernate.initialize(a.getTipoAvion());
            }

            tx.commit();
        } catch (Exception e) { 
           if (tx != null) {
             tx.rollback();
             throw e;
           }
        }  finally { 
             session.close();
            return a;
        }
    }
    
//    public ArrayList<Avion> listarAvion() {
//        ArrayList<Avion> lstAvion = new ArrayList<Avion>();
//        
//        Session session = HibernateUtil.getSessionFactory().openSession();
//        Transaction tx = session.beginTransaction();        
//        List lista = session.createQuery("from Avion").list();
//        for ( Iterator iter = lista.iterator(); iter.hasNext(); ) {    
//            Avion doc = (Avion) iter.next();
//            lstAvion.add(doc);
//        }        
//        tx.commit();
//        session.close();
//        return lstAvion;
//    }
//        
//    public void actualizarAvion(Avion c) {
//        Session session = HibernateUtil.getSessionFactory().openSession();
//        Transaction tx =  session.beginTransaction();
//            session.update(c);
//        tx.commit();
//        session.close();
//    }
}

